//
// File: sum.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 25-Apr-2025 12:42:26
//

// Include Files
#include "sum.h"
#include "rt_nonfinite.h"
#include "coder_array.h"
#include <cstring>
#include <string.h>

// Function Definitions
//
// Arguments    : const ::coder::array<double, 2U> &x
//                ::coder::array<double, 2U> &y
// Return Type  : void
//
namespace coder {
void sum(const ::coder::array<double, 2U> &x, ::coder::array<double, 2U> &y)
{
  int vlen;
  vlen = x.size(0);
  if ((x.size(0) == 0) || (x.size(1) == 0)) {
    y.set_size(1, x.size(1));
    vlen = x.size(1);
    for (int npages{0}; npages < vlen; npages++) {
      y[npages] = 0.0;
    }
  } else {
    int npages;
    npages = x.size(1);
    y.set_size(1, x.size(1));
    for (int xi{0}; xi < npages; xi++) {
      int xpageoffset;
      xpageoffset = xi * x.size(0);
      y[xi] = x[xpageoffset];
      for (int k{2}; k <= vlen; k++) {
        y[xi] = y[xi] + x[(xpageoffset + k) - 1];
      }
    }
  }
}

//
// Arguments    : const ::coder::array<double, 2U> &x
//                double y[449]
// Return Type  : void
//
void sum(const ::coder::array<double, 2U> &x, double y[449])
{
  if (x.size(1) == 0) {
    std::memset(&y[0], 0, 449U * sizeof(double));
  } else {
    int firstBlockLength;
    int lastBlockLength;
    int nblocks;
    int xoffset;
    if (x.size(1) <= 1024) {
      firstBlockLength = x.size(1);
      lastBlockLength = 0;
      nblocks = 1;
    } else {
      firstBlockLength = 1024;
      nblocks = x.size(1) / 1024;
      lastBlockLength = x.size(1) - (nblocks << 10);
      if (lastBlockLength > 0) {
        nblocks++;
      } else {
        lastBlockLength = 1024;
      }
    }
    for (int xj{0}; xj < 449; xj++) {
      y[xj] = x[xj];
    }
    for (int k{2}; k <= firstBlockLength; k++) {
      xoffset = (k - 1) * 449;
      for (int xj{0}; xj < 449; xj++) {
        y[xj] += x[xoffset + xj];
      }
    }
    for (int ib{2}; ib <= nblocks; ib++) {
      double bsum[449];
      int hi;
      firstBlockLength = (ib - 1) * 459776;
      for (int xj{0}; xj < 449; xj++) {
        bsum[xj] = x[firstBlockLength + xj];
      }
      if (ib == nblocks) {
        hi = lastBlockLength;
      } else {
        hi = 1024;
      }
      for (int k{2}; k <= hi; k++) {
        xoffset = firstBlockLength + (k - 1) * 449;
        for (int xj{0}; xj < 449; xj++) {
          bsum[xj] += x[xoffset + xj];
        }
      }
      for (int xj{0}; xj < 449; xj++) {
        y[xj] += bsum[xj];
      }
    }
  }
}

} // namespace coder

//
// File trailer for sum.cpp
//
// [EOF]
//
